import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { NzMessageService, NzModalService, UploadChangeParam, UploadFile } from 'ng-zorro-antd';
import { FetchService } from '../../../../core/net/fetch.service';
import * as fp from 'lodash/fp';

@Component({
    selector: 'app-pm-flood-plan',
    templateUrl: './pm-flood-plan.component.html',
    styleUrls: ['./pm-flood-plan.component.scss']
})
export class PmFloodPlanComponent implements OnInit {
    planList = [];
    form: FormGroup;
    modalTitle: string = '';
    modalShow: boolean = false;
    fileList: UploadFile[] = [];
    uploadUrl = `${(window as any).environment.apiBaseUrl}comm/file/upload`;
    fileSrc = '';
    currentFileId = null;
    constructor(private fb: FormBuilder,
                private message: NzMessageService,
                private modalService: NzModalService,
                private fetchService: FetchService) {
        this.form = this.fb.group({
            id: [null],
            planName: ['']
        });
    }

    ngOnInit(): void {
        this.queryInfo();
    }

    queryInfo(): void {
        this.fetchService.post('projectManage/wrsPlanB/list', {
            pageSize: 10000
        }).subscribe(res => {
            this.planList = res['list'];
            this.getPlanPreviews(this.planList[0]);
        });
    }

    getPlanPreviews(file): void {
        this.currentFileId = file.id;
        this.fileSrc = `${(window as any).environment.apiBaseUrl}comm/file/view/${file.fileId}`;
        // this.fileSrc = `${(window as any).environment.apiBaseUrl}projectManage/wrsPlanB/${file.id}`;
    }

    handleChange({ file, fileList }: UploadChangeParam): void {
        const status = file.status;
        if (status !== 'uploading') {
            // console.log(file, fileList);
        }
        if (status === 'done') {
            // console.log(file, fileList);
            this.message.success(`${file.name} 文件上传成功`);
            this.form.patchValue({
                fileName: file.name
            });
            this.fileList = fileList;
        } else if (status === 'error') {
            this.message.success(`${file.name} 文件上传失败，请重试`);
        }
    }

    openModal(): void {
        this.modalTitle = '新增';
        this.modalShow = true;
    }

    onConfirm() {
        const data = {
            ...{
                planName: this.fileList[0].response.data.fileName,
                planFile: this.fileList[0].response.data.filePath,
                fileId: this.fileList[0].response.data.id,
            },
            ...this.form.value
        };
        const url = data.id ? 'projectManage/wrsPlanB/edit' : 'projectManage/wrsPlanB/add';
        this.fetchService.post(url, data).subscribe(res => {
            this.message.success('提交成功');
            this.modalShow = false;
            this.queryInfo();
            this.form.reset();
        });
    }

    onCancel() {
        this.fileList = [];
        this.form.reset();
        this.modalShow = false;
    }

    deleteFile() {
        this.modalService.confirm({
            nzTitle: "确认删除",
            nzContent: "确认删除该文件吗？",
            nzOnOk: () => {
                this.fetchService
                    .get(`projectManage/wrsPlanB/remove/${this.currentFileId}`)
                    .subscribe(res => {
                        this.queryInfo();
                    });
            }
        });
    }
}
